package com.lorianknights.rpg.screens
{
	
	import flash.display.MovieClip;
	import flash.events.Event;
	import flash.events.IOErrorEvent;
	import flash.events.MouseEvent;
	import flash.net.URLLoader;
	import flash.text.TextField;
	import com.lorianknights.rpg.screens.Screen;
	import com.lorianknights.rpg.utils.Constants;
	import com.lorianknights.rpg.screens.skins.ScreenForgotPasswordSkin;
	import com.lorianknights.rpg.utils.Constants;
	import com.lorianknights.rpg.utils.Utility;
	
	public class ScreenForgotPassword extends Screen
	{
		
		private var skin:ScreenForgotPasswordSkin;
		public static const DEFAULT_ENTRY:String = Constants.FORGOT_PASSWORD_DEFAULT_MSG;
		private var fpLoader:URLLoader = new URLLoader();
		
		public function ScreenForgotPassword()
		{
			
			super();
			
		}
		
		public override function init():void
		{
			
			var assetClass:Class = null;
			
			this.skin = new ScreenForgotPasswordSkin();
			addChild(this.skin);
			
				if (this.skin == null)
				{
					
					assetClass = getDefinitionByName(Constants.FORGOT_PASSWORD_SKIN) as Class;
					this.skin.btnSend = this.skin.getChildByName(Constants.BTN_SEND_STR) as MovieClip;
					this.skin.sending_txt = this.skin.getChildByName(Constants.STR_SENDING_TXT) as TextField;
					this.skin = addChildAt(new assetClass(), 0) as MovieClip;
					
						if (this.skin.numChildren > 0)
						{
							
							addChild(this.skin);
							
						}
					
				}
				
				this.skin.btnSend.addEventListener(MouseEvent.CLICK, onSendClick);
				this.skin.btnSend.addEventListener(MouseEvent.ROLL_OVER, onRollOverFP);
				this.skin.btnSend.addEventListener(MouseEvent.ROLL_OUT, onRollOutFP);
					
		}
		
		private function onRollOverFP(event:MouseEvent):void
		{
			
			var mc:MovieClip = event.currentTarget as MovieClip;
			
				if (mc.currentLabel != Constants.IN)
				{
					
					mc.gotoAndPlay(Constants.IN);
					
				}
		}
		
		private function onRollOutFP(event:MouseEvent):void
		{
			
			var mc:MovieClip = event.currentTarget as MovieClip;
			
				if (mc.currentLabel != Constants.OUT)
				{
					
					mc.gotoAndPlay(Constants.OUT);
					
				}
		}
		
		private function onSendClick(event:MouseEvent):void
		{
			
			var s:String = this.skin.sending_txt.text;
			this.sendFPRequest(s)
		
		}
		
		private function sendFPRequest(dataStr:String):void
		{
			
			var variables:URLVariables = null;
			var request:URLRequest = null;
			var fpURL:String = Game.BASE_URL + Constants.FORGOT_PASSWORD_PHP;
			
			variables = new URLVariables();
			request = new URLRequest(fpURL);
			request.data = variables;
			this.loader.load(request);
			this.loader.dataFormat = URLLoaderDataFormat.VARIABLES;
			
				try 
				{
					
					this.loader.addEventListener(Event.COMPLETE, onFPSendComplete);
					this.loader.addEventListener(IOErrorEvent.IO_ERROR, onFPError);
					
				} catch (e:Error) { }
				
		}
		
		private function onFPError(event:IOErrorEvent):void
		{
			
			var pop:DialoguePopup = new DialoguePopup();
			pop.showMessage(Constants.ERROR_MSG, Constants.ORIGINAL_ERROR_1);
			addChild(pop);
			Utility.traceMessage("FP Error: " + event);
			
		}

		private function onFPSendComplete(event:Event):void
		{
			
			var o:Object = Utility.convertXMLToObject(XML(event.target.data));
			var pop:DialoguePopup = new DialoguePopup();
			
				if (pop != null)
				{
					
					pop.init();
					
					if (o.intSuccess == 1)
					{
						
						pop.populatePopup(Constants.SUCCESS_MSG, Constants.ORIGINAL_SUCCESS_1 + o.strEmail + Constants.EXCLAMATION_PT);
						
					} else {
						
						pop.populatePopup(Constants.ERROR_MSG, Constants.ORIGINAL_ERROR_2);
						
					}
				}
				
		}
	}
}
			